<template>
    <div class="register-box">
        <div class="register">
            <div class="header">
                <h3>注册</h3>
            </div>
            <el-form id="register_form" ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules"
                label-width="auto" status-icon>
                <el-form-item class="form-group" prop="username">
                    <label for="username">用户名</label>
                    <el-input type="text" class="form-control" id="username" name="username" v-model="ruleForm.username"
                        placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item class="form-group" prop="nickName">
                    <label for="nickname">昵称</label>
                    <el-input type="text" class="form-control" id="nickname" name="nickname" v-model="ruleForm.nickName"
                        placeholder="请输入昵称" />
                </el-form-item>
                <el-form-item class="form-group" prop="password1">
                    <label for="password">密码</label>
                    <el-input type="password" class="form-control" id="password" name="password"
                        v-model="ruleForm.password1" placeholder="请输入密码" />
                </el-form-item>
                <el-form-item class="form-group" prop="password2">
                    <label for="password">确定密码</label>
                    <el-input type="password" class="form-control" id="password" name="password"
                        v-model="ruleForm.password2" placeholder="确定密码" @blur="checkPasswords" />
                </el-form-item>
                <el-button type="primary" style="margin-top: 10px;width: 50%;" round @click="submitForm(ruleFormRef)">
                    注册<el-icon>
                        <Plus />
                    </el-icon>
                </el-button>
                <!-- <el-input type="submit" value="tijiao" /> -->
            </el-form>
            <div class="message">
                <p>已有账号? <router-link to="/login">点击登录</router-link>.</p>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">

import { userRegister } from "@/api/user"
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
import type {FormInstance, FormRules } from 'element-plus'
import router from "@/router";

// 注册对象接口
interface RuleForm {
    username: string
    nickName: string
    password1: string
    password2: string
}

//引用表单ref
const ruleFormRef = ref<FormInstance>()


// 表单对象
const ruleForm = reactive<RuleForm>({
    username: '',
    nickName: '',
    password1: '',
    password2: '',
})


//验证密码
const checkPasswords = () => {
    if (ruleForm.password1 !== ruleForm.password2) {
        return false
    }
    return true
}

// 表单验证规则
const rules = reactive<FormRules<RuleForm>>({
    username: [
        { required: true, message: '账号不能为空！', trigger: 'blur' },
        { min: 3, max: 15, message: '请输入有效账号！', trigger: 'blur' },
    ],
    nickName: [
        { required: true, message: '昵称不能为空！', trigger: 'blur' },
        { min: 2, max: 15, message: '请输入有效昵称！', trigger: 'blur' },
    ],
    password1: [
        { required: true, message: '密码不能为空！', trigger: 'blur' },
        { min: 3, message: '密码长度至少大于3字符！', trigger: 'blur' },
    ],
    password2: [
        { required: true, message: '请再次确认密码！', trigger: 'blur' },
        { validator: checkPasswords, message: '两次密码不一致！', trigger: 'blur' },
    ]
})

// 提交表单
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl)
        return
    await formEl.validate((valid, fields) => {
        if (valid) {
            const user = {
                username: ruleForm.username,
                password: ruleForm.password2,
                nickname: ruleForm.nickName,
            }
            userRegister(user).then(res => {
                if (res.flag) {
                    ElMessage({
                        message: res.message,
                        type: 'success',
                    })
                    router.push({path: '/login'})
                } else {
                    ElMessage({
                        message: res.message,
                        type: 'error',
                    })
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}


</script>

<style scoped>
:deep(.el-form-item__error) {
    top: 110%;
    width: 100%;
    text-align: left;
    padding-left: 25%;
    color: rgb(195, 5, 5);
}

.register-box {
    box-sizing: border-box;
    padding-top: 150px;
    background: url("@/assets/img/rice.png");
    background-size: cover;
    width: 100%;
    height: 100%;

    .register {
        width: 340px;
        margin: 0 auto;
        font-weight: bold;
        color: aliceblue;

        .header {
            height: 28px;
            text-align: center;
            color: #024c85;
        }

        #register_form {
            padding: 20px;
            margin-bottom: 15px;
            border-radius: 5px;

            .form-group {
                display: flex;
                width: 100%;
                height: 30px;
                margin-bottom: 20px;
                line-height: 30px;

                .form-control {
                    width: 80%;
                    padding-left: 10px
                }

                label {
                    width: 20%;
                    ;
                }
            }
        }

        .message {
            padding: 10px;
            padding-bottom: 0;
            color: white;
            border-radius: 5px;
            text-align: center;
        }
    }
}
</style>